//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
//引用腾讯地图API
var QQMapWX = require('../../common/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    // 上拉加载
    allGoodsData: [],
    pageIndex: 1,
    state: "",
    // 商品类别
    index: 0,
    goodsType: ['全部', '医药保健', '数码电子', '美妆护肤', '珠宝首饰', '餐饮美食', '休闲旅游', '母婴用品', '服装/内衣', '买房租房', '农副产品', '鞋帽箱包', '酒店连锁', '卫浴厨具', '图书教材', '家居装修', '家装家居', '护理美容', '烟酒/超市', '汽车用品', '其他'],
    searchKey: '',
    businessType: 0,
    // 
    hasLocation: false,
    X: 0,
    Y: 0,
    city: '城市',
    // 
    // 
    region: ['广东省', '广州市', '海珠区'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 定位实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'b6af1635786ff310b0f07cba63460532'
    });
    qqmapsdk.reverseGeocoder({
      poi_options: 'policy=2',
      success: function (res) {
        console.log(res)
        that.setData({
          X: res.result.location.lng,
          Y: res.result.location.lat,
          region: [res.result.address_component.province, res.result.address_component.city, res.result.address_component.district]
        })
        that.GetEnjoyAreaBusiness();
      },
      fail: function (res) {
        // wx.showModal({
        //   content: '此功能需要定位权限，请点击左上角重新定位',
        //   confirmText: "确认",
        //   cancelText: "取消",
        //   success: function (res) {
        //     console.log(res);
        //     if (res.confirm) {

        //     } else {
        //       console.log('用户点击取消')
        //     }
        //   }
        // });
      },
      complete: function (res) {
        // console.log(res);

      }
    });
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.GetEnjoyAreaBusiness();
  },

  bindPickerChange: function (e) {
    console.log(e);
    var that = this;
    var pageIndex = this.data.pageIndex;
    var allGoodsData = this.data.allGoodsData;
    var businessType = this.data.businessType;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      businessType: e.detail.value,
      allGoodsData:[]
    })
    pageIndex = 0;
    this.GetEnjoyAreaBusiness();
  },
  // 
  GetEnjoyAreaBusiness: function () {
    wx.showLoading({
      title: '加载中',
      icon: 'success',
      duration: 2000
    })
    var that = this;
    var allGoodsData = this.data.allGoodsData;
    var pageIndex = this.data.pageIndex;
    var businessType = this.data.businessType;
    var region = this.data.region;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetEnjoyAreaBusiness&PageIndex=" + pageIndex + "&PageSize=10&City=" + region[1] + "&Area=&BusinessType=" + businessType),
      success: function (res) {
        console.log("method=GetEnjoyAreaBusiness&PageIndex=" + pageIndex + "&PageSize=10&City=" + region[1] + "&Area=" + region[2] + "&BusinessType=" + businessType);
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        for (var i = 0; i < json.length; i++) {
          allGoodsData.push(json[i])
        }
        that.setData({
          allGoodsData: allGoodsData,
        })
        console.log(allGoodsData);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();

      }
    })
  },
  // 省市区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      pageIndex: 1,
      allGoodsData: [],
    })
    this.GetEnjoyAreaBusiness();
  },
  // 
})